package com.zjb.home.boxingtu.view.other;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zjb.home.boxingtu.view.BaseView;

import java.util.Random;

/**
 * Created by zhangjiebo on 2019/12/16.
 *
 * @author ZhangJieBo
 */
public class ZhiBoIngView extends BaseView {

    private Paint paint;
    private float[] fuDu = new float[]{0, 0.5f, 0.2f};
    private float[] fuDuEnd = new float[]{0.8f, 0, 0.8f};

    private ObjectAnimator animator;

    public ZhiBoIngView(@NonNull Context context) {
        super(context);
    }

    public ZhiBoIngView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ZhiBoIngView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initAttrs(AttributeSet attrs) {

    }

    @Override
    public void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#D93167"));
        paint.setStyle(Paint.Style.FILL);
    }

    {
        animator = ObjectAnimator.ofObject(this, "fuDu", new HsvEvaluator(), fuDu, fuDuEnd);
        animator.setDuration(100);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                super.onAnimationRepeat(animation);
                for (int i = 0; i < fuDuEnd.length; i++) {
                    float nextFloat = new Random().nextFloat();
                    fuDuEnd[i] = nextFloat;
                }
                animator.setObjectValues(fuDu, fuDuEnd);
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float dan = getWidth() / 5f;
        canvas.drawRect(0, getHeight()* fuDu[0], dan, getHeight(), paint);
        canvas.drawRect(dan * 2f, getHeight()* fuDu[1], dan * 3f, getHeight(), paint);
        canvas.drawRect(dan * 4f, getHeight()* fuDu[2], dan * 5f, getHeight(), paint);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        animator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        animator.end();
    }

    public void setFuDu(float[] fuDu) {
        this.fuDu = fuDu;
        invalidate();
    }

    // 自定义 HslEvaluator
    private class HsvEvaluator implements TypeEvaluator<float[]> {
        float[] outValue = new float[3];

        @Override
        public float[] evaluate(float fraction, float[] startValue, float[] endValue) {
            for (int i = 0; i < startValue.length; i++) {
                outValue[i] = startValue[i] + (endValue[i] - startValue[i]) * fraction;
            }
            return outValue;
        }
    }
}
